package com.hdoit.ares.selfcommon.configurations;

import java.io.FileNotFoundException;

import org.springframework.util.ResourceUtils;

import com.hdoit.ares.selfcommon.configurations.model.WechatConfigModel;
import com.tennetcn.common.message.ConfigurationBase;

public class WechatConfiguration extends ConfigurationBase{
	public WechatConfiguration(String path){
		super(path);
	}
	
    private static WechatConfiguration configuration;
    
    public WechatConfigModel wechatConfigModel;
    
    public static WechatConfiguration getInstance(){
    	if(configuration==null){
    		configuration=new WechatConfiguration("wechat.properties");
    		configuration.resolveConfigModel();
    	}
    	return configuration;
    }
    
    public WechatConfigModel resolveConfigModel(){
		if(wechatConfigModel!=null){
			return wechatConfigModel;
		}
		wechatConfigModel=new WechatConfigModel();
		
		wechatConfigModel.setAppId(configuration.getValue("wechat.appId"));
		wechatConfigModel.setSecret(configuration.getValue("wechat.secret"));
		wechatConfigModel.setToken(configuration.getValue("wechat.token"));
		wechatConfigModel.setAesKey(configuration.getValue("wechat.aesKey"));
		wechatConfigModel.setPublicUrl(configuration.getValue("publicUrl"));
		
		wechatConfigModel.setMchId(configuration.getValue("wechat.pay.mchId"));
		wechatConfigModel.setMchKey(configuration.getValue("wechat.pay.mchKey"));
		wechatConfigModel.setSubAppId(configuration.getValue("wechat.pay.subAppId"));
		wechatConfigModel.setSubMchId(configuration.getValue("wechat.pay.subMchId"));
		
		String keypath=configuration.getValue("wechat.pay.keyPath");
		if(keypath.indexOf("classpath:")>=0){
			try {
				String classpath = ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath();
				keypath=keypath.replace("classpath:", classpath);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		wechatConfigModel.setKeyPath(keypath);
		
		wechatConfigModel.setOrgCode(configuration.getValue("wechat.pay.orgCode"));
		wechatConfigModel.setSpbillCreateIp(configuration.getValue("wechat.pay.spbillCreateIp"));
		wechatConfigModel.setNotifyURL(configuration.getValue("wechat.pay.notifyURL"));
		wechatConfigModel.setTradeType(configuration.getValue("wechat.pay.tradeType"));
		
		return wechatConfigModel;
	}
}
